<?php

require 'core.php';

class Images
{

    private $template = "";
    private $album = "";
    private $cache = "";
    private $imgsize = array("width"=>100,"height"=>100);
    private $maximagesize = 0;

    function  __construct()
    {
        $this->template = MODULE_FOLDER.DIR_SEPARATOR."images".DIR_SEPARATOR."module.tpl";
        $this->album = "upload".DIR_SEPARATOR.substr(User::$id, 0, 2).DIR_SEPARATOR.User::$id;
        $this->cache = $this->album.DIR_SEPARATOR."cache";

        $this->maximagesize = 1024*1024*10;

        Renderer::$var["module_tpl"] = $this->template;
        Renderer::$var["album_dir"] = $this->album;
        Renderer::$var["cache_dir"] = $this->cache;
        Renderer::$var["imgsize"] = $this->imgsize;
        Renderer::$var["maximagesize"] = $this->maximagesize;
    }

    public function process()
    {
        if(Route::$action)
            $action = Route::$action;
        else
            $action = "getalbum";

        if(method_exists($this, $action))
        {
            $refl = new ReflectionMethod(__CLASS__, $action);
            if(! $refl->isPublic())
                ErrorHandler::RaiseFatal("Action ".$action." not found in ".Route::$module.". AD.");

            $this->$action();
        }
        else
            ErrorHandler::RaiseFatal("Action ".$action." not found in ".Route::$module);
    }

    public function getalbum()
    {
        if(is_dir($this->cache)) 
        {
            Renderer::RegisterVariableByRef("pictures_list", Files::GetFileList($this->album,"/[a-z0-9_-]+/", fcBaseInfo, ftFiles, true, true));
        } else
        {
            Renderer::$var["pictures_list"] = array();
        }

        Renderer::Render("index.tpl");
    }

    public function upload()
    {

        if(isset(Context::$uploadedFiles["pict"]))
        {
            if(! is_dir($this->cache))
                Files::CreateDir($this->cache);

            $newname = microtime(true)."_".Context::$uploadedFiles["pict"]->name;
            $id = "_".md5($this->album.DIR_SEPARATOR.$newname);
            $file = Context::$uploadedFiles["pict"]->moveTo($this->album, $newname);
            $file->GetFullInfo();
            if((! $file->info) || $file->size > $this->maximagesize)
            {
                $file->Delete();
                if(Context::$isAjax)
                {
                    echo "<script type=\"text/javascript\">window.parent.uploadError();</script>";
                    die;
                }
            }

            if(Context::$isAjax)
            {
                echo "<script type=\"text/javascript\">window.parent.addImage('",$newname,"','",$id,"','",$this->album,"/',",$this->imgsize["width"],",",$this->imgsize["height"],");</script>";
                die;
            }
        }


        Site::Redirect(Context::$request["host"]);
    }

    public function auth()
    {

        if(preg_match("/^[a-z0-9]{5,64}$/", Context::$get["link"]))
        {
            Context::$cookies["auth"] = Context::$get["link"];
        }

        Site::Redirect(Context::$request["host"]);
    }

    public function deleteall()
    {

        foreach(Files::GetFileList($this->album,"/[a-z0-9_-]+/", fcBaseInfo, ftFiles) as $file)
        {
            $file->Delete();
            if($thumbfile = Files::GetFile($this->cache.DIR_SEPARATOR.$file->basename, fcFast))
                $thumbfile->Delete();
        }

        if(Context::$isAjax)
            die;

        Site::Redirect(Context::$request["host"]);
    }

    public function delete()
    {

        $filename = preg_replace("/[\\\\\/\?\*{}]*/","", Context::$get["i"]);

        if($file = Files::GetFile($this->album.DIR_SEPARATOR.$filename, fcFast))
        {
            $file->Delete();
            if($thumbfile = Files::GetFile($this->cache.DIR_SEPARATOR.$filename, fcFast))
                $thumbfile->Delete();

            if(Context::$isAjax)
                die;
        }

        Site::Redirect(Context::$request["host"]);
    }

    public function thumb()
    {
        if(! is_dir($this->cache))
            Files::CreateDir($this->cache);
        
        $filename = preg_replace("/[\\\\\/\?\*{}]*/","", Context::$get["i"]);
        $file = $this->album.DIR_SEPARATOR.$filename;
        $thumbfile = $this->cache.DIR_SEPARATOR.$filename;

        RGalleryCore::Init();
        RGalleryCore::SetDir(Context::$request["rootdir"].DIR_SEPARATOR.$this->album, Context::$request["rootdir"].DIR_SEPARATOR.$this->cache);

        if( !file_exists($thumbfile))
        {
            $im = RGalleryCore::GetThumb($filename, $this->imgsize["width"], $this->imgsize["height"]);
            $thumbfile = $im["thumb"];
        }

        $mimeType = "image/jpeg";
        $filename = "thumb.jpg";

        if (file_exists($thumbfile))
        {

            $time=date('r',filemtime($thumbfile));
            
            header("Content-Type: $mimeType");
            header('Cache-Control: public, must-revalidate, max-age=0');
            header('Pragma: no-cache');
            header("Content-Disposition: inline; filename=$filename");
            header("Content-Transfer-Encoding: binary\n");
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header("Last-Modified: $time");
            header('Connection: close');
            readfile($thumbfile);
            die;
        }
    }

}

?>
